################
#PSRM: Explaining Support for Redistribution: Social Insurance Systems and Fairness
#
#Observational Data
#Part VII: Occupational unemployment rates
#
#Verena Fetscher
#July 2022
####################

rm(list=ls())

##########################
#Load Data
##########################

our<-read.table("cntry_isco2d_our",header=T)
names(our)<-c("cntry","isco2d","our")

load("DataFile_07_Replacement.Rda")


table(data$risk)

##########################
#Occupational unemployment risk (isco2d)
##########################
#ESS1-ESS5
table(data$iscoco)
table(str_extract(data$iscoco, "^.{2}"))
data$isco2d<-NA
data$isco2d<-str_extract(data$iscoco, "^.{2}")


#ESS6-ESS7
data$isco08_num<-(as.numeric(as.factor(data$isco08)))

table(as.numeric(data$isco08)[data$isco08=="Chief executives, senior officials and legislators"])
table(as.numeric(data$isco08)[data$isco08=="Managing directors and chief executives"])
table(data$isco08)[9:15]

for(i in c(9:15)){
  data$isco2d[data$isco08_num==i]<-11
}

table(data$isco2d)


table(as.numeric(data$isco08)[data$isco08=="Administrative and commercial managers"])
table(as.numeric(data$isco08)[data$isco08=="Research and development managers"])

table(data$isco08)[16:25]

for(i in c(16:25)){
  data$isco2d[data$isco08_num==i]<-12
}



table(as.numeric(data$isco08)[data$isco08=="Production and specialised services managers"])
table(as.numeric(data$isco08)[data$isco08=="Professional services managers not elsewhere classified"])

table(data$isco08)[26:43]

for(i in c(26:43)){
  data$isco2d[data$isco08_num==i]<-13
}


table(as.numeric(data$isco08)[data$isco08=="Hospitality, retail and other services managers"])
table(as.numeric(data$isco08)[data$isco08=="Services managers not elsewhere classified"])

table(data$isco08)[44:51]

for(i in c(44:51)){
  data$isco2d[data$isco08_num==i]<-14
}



table(as.numeric(data$isco08)[data$isco08=="Science and engineering professionals"])
table(as.numeric(data$isco08)[data$isco08=="Graphic and multimedia designers"])

table(data$isco08)[53:82]

for(i in c(44:51)){
  data$isco2d[data$isco08_num==i]<-21
}


table(as.numeric(data$isco08)[data$isco08=="Health professionals"])
table(as.numeric(data$isco08)[data$isco08=="Health professionals not elsewhere classified"])

table(data$isco08)[83:101]

for(i in c(83:101)){
  data$isco2d[data$isco08_num==i]<-22
}


table(as.numeric(data$isco08)[data$isco08=="Teaching professionals"])
table(as.numeric(data$isco08)[data$isco08=="Teaching professionals not elsewhere classified"])

table(data$isco08)[102:116]

for(i in c(102:116)){
  data$isco2d[data$isco08_num==i]<-23
}


table(as.numeric(data$isco08)[data$isco08=="Business and administration professionals"])
table(as.numeric(data$isco08)[data$isco08=="Information and communications technology sales professional"])

table(data$isco08)[117:131]

for(i in c(117:131)){
  data$isco2d[data$isco08_num==i]<-24
}



table(as.numeric(data$isco08)[data$isco08=="Information and communications technology professionals"])
table(as.numeric(data$isco08)[data$isco08=="Database and network professionals not elsewhere classified"])

table(data$isco08)[132:143]

for(i in c(117:131)){
  data$isco2d[data$isco08_num==i]<-25
}


table(as.numeric(data$isco08)[data$isco08=="Legal, social and cultural professionals"])
table(as.numeric(data$isco08)[data$isco08=="Creative and performing artists not elsewhere classified"])

table(data$isco08)[144:170]

for(i in c(117:131)){
  data$isco2d[data$isco08_num==i]<-26
}


table(as.numeric(data$isco08)[data$isco08=="Science and engineering associate professionals"])
table(as.numeric(data$isco08)[data$isco08=="Air traffic safety electronics technicians"])

table(data$isco08)[172:203]

for(i in c(117:131)){
  data$isco2d[data$isco08_num==i]<-31
}

table(as.numeric(data$isco08)[data$isco08=="Health associate professionals"])
table(as.numeric(data$isco08)[data$isco08=="Health associate professionals not elsewhere classified"])

table(data$isco08)[204:224]

for(i in c(204:224)){
  data$isco2d[data$isco08_num==i]<-32
}


table(as.numeric(data$isco08)[data$isco08=="Business and administration associate professionals"])
table(as.numeric(data$isco08)[data$isco08=="Regulatory government associate professionals not elsewhere"])

table(data$isco08)[225:254]

for(i in c(225:254)){
  data$isco2d[data$isco08_num==i]<-33
}



table(as.numeric(data$isco08)[data$isco08=="Legal, social, cultural and related associate professionals"])
table(as.numeric(data$isco08)[data$isco08=="Other artistic and cultural associate professionals"])

table(data$isco08)[255:269]

for(i in c(255:269)){
  data$isco2d[data$isco08_num==i]<-34
}


table(as.numeric(data$isco08)[data$isco08=="Information and communications technicians"])
table(as.numeric(data$isco08)[data$isco08=="Telecommunications engineering technicians"])

table(data$isco08)[270:278]

for(i in c(270:278)){
  data$isco2d[data$isco08_num==i]<-35
}


table(as.numeric(data$isco08)[data$isco08=="General and keyboard clerks"])
table(as.numeric(data$isco08)[data$isco08=="Data entry clerks"])

table(data$isco08)[280:285]

for(i in c(280:285)){
  data$isco2d[data$isco08_num==i]<-41
}


table(as.numeric(data$isco08)[data$isco08=="Customer services clerks"])
table(as.numeric(data$isco08)[data$isco08=="Client information workers not elsewhere classified"])

table(data$isco08)[286:300]

for(i in c(286:300)){
  data$isco2d[data$isco08_num==i]<-42
}


table(as.numeric(data$isco08)[data$isco08=="Numerical and material recording clerks"])
table(as.numeric(data$isco08)[data$isco08=="Transport clerks"])

table(data$isco08)[301:309]

for(i in c(301:309)){
  data$isco2d[data$isco08_num==i]<-43
}


table(as.numeric(data$isco08)[data$isco08=="Other clerical support workers"])
table(as.numeric(data$isco08)[data$isco08=="Clerical support workers not elsewhere classified"])

table(data$isco08)[310:318]

for(i in c(310:318)){
  data$isco2d[data$isco08_num==i]<-44
}


table(as.numeric(data$isco08)[data$isco08=="Personal service workers"])
table(as.numeric(data$isco08)[data$isco08=="Personal services workers not elsewhere classified"])

table(data$isco08)[320:342]

for(i in c(320:342)){
  data$isco2d[data$isco08_num==i]<-51
}


table(as.numeric(data$isco08)[data$isco08=="Sales workers"])
table(as.numeric(data$isco08)[data$isco08=="Sales workers not elsewhere classified"])

table(data$isco08)[343:359]

for(i in c(343:359)){
  data$isco2d[data$isco08_num==i]<-51
}


table(as.numeric(data$isco08)[data$isco08=="Personal care workers"])
table(as.numeric(data$isco08)[data$isco08=="Personal care workers in health services not elsewhere class"])

table(data$isco08)[360:367]

for(i in c(360:367)){
  data$isco2d[data$isco08_num==i]<-53
}


table(as.numeric(data$isco08)[data$isco08=="Protective services workers"])
table(as.numeric(data$isco08)[data$isco08=="Protective services workers not elsewhere classified"])

table(data$isco08)[369:374]

for(i in c(369:374)){
  data$isco2d[data$isco08_num==i]<-54
}


table(as.numeric(data$isco08)[data$isco08=="Market-oriented skilled agricultural workers"])
table(as.numeric(data$isco08)[data$isco08=="Mixed crop and animal producers"])

table(data$isco08)[376:387]

for(i in c(376:387)){
  data$isco2d[data$isco08_num==i]<-61
}


table(as.numeric(data$isco08)[data$isco08=="Market-oriented skilled forestry, fishery and hunting worker"])
table(as.numeric(data$isco08)[data$isco08=="Hunters and trappers"])

table(data$isco08)[388:394]

for(i in c(388:394)){
  data$isco2d[data$isco08_num==i]<-62
}


table(as.numeric(data$isco08)[data$isco08=="Subsistence farmers, fishers, hunters and gatherers"])
table(as.numeric(data$isco08)[data$isco08=="Subsistence fishers, hunters, trappers and gatherers"])

table(data$isco08)[395:399]

for(i in c(395:399)){
  data$isco2d[data$isco08_num==i]<-63
}


table(as.numeric(data$isco08)[data$isco08=="Building and related trades workers, excluding electricians"])
table(as.numeric(data$isco08)[data$isco08=="Building structure cleaners"])

table(data$isco08)[401:420]

for(i in c(401:420)){
  data$isco2d[data$isco08_num==i]<-71
}


table(as.numeric(data$isco08)[data$isco08=="Metal, machinery and related trades workers"])
table(as.numeric(data$isco08)[data$isco08=="Bicycle and related repairers"])

table(data$isco08)[421:437]

for(i in c(421:437)){
  data$isco2d[data$isco08_num==i]<-72
}


table(as.numeric(data$isco08)[data$isco08=="Handicraft and printing workers"])
table(as.numeric(data$isco08)[data$isco08=="Print finishing and binding workers"])

table(data$isco08)[438:452]

for(i in c(438:452)){
  data$isco2d[data$isco08_num==i]<-73
}


table(as.numeric(data$isco08)[data$isco08=="Electrical and electronic trades workers"])
table(as.numeric(data$isco08)[data$isco08=="Information and communications technology installers and ser"])

table(data$isco08)[453:460]

for(i in c(453:460)){
  data$isco2d[data$isco08_num==i]<-74
}


table(as.numeric(data$isco08)[data$isco08=="Food processing, wood working, garment and other craft and r"])
table(as.numeric(data$isco08)[data$isco08=="Craft and related workers not elsewhere classified"])

table(data$isco08)[461:485]

for(i in c(461:485)){
  data$isco2d[data$isco08_num==i]<-75
}


table(as.numeric(data$isco08)[data$isco08=="Stationary plant and machine operators"])
table(as.numeric(data$isco08)[data$isco08=="Stationary plant and machine operators not elsewhere classif"])

table(data$isco08)[487:520]

for(i in c(487:520)){
  data$isco2d[data$isco08_num==i]<-81
}


table(as.numeric(data$isco08)[data$isco08=="Assemblers"])
table(as.numeric(data$isco08)[data$isco08=="Assemblers not elsewhere classified"])

table(data$isco08)[521:525]

for(i in c(521:525)){
  data$isco2d[data$isco08_num==i]<-82
}


table(as.numeric(data$isco08)[data$isco08=="Drivers and mobile plant operators"])
table(as.numeric(data$isco08)[data$isco08=="Ships' deck crews and related workers"])

table(data$isco08)[526:541]

for(i in c(526:541)){
  data$isco2d[data$isco08_num==i]<-83
}


table(as.numeric(data$isco08)[data$isco08=="Cleaners and helpers"])
table(as.numeric(data$isco08)[data$isco08=="Other cleaning workers"])

table(data$isco08)[543:551]

for(i in c(543:551)){
  data$isco2d[data$isco08_num==i]<-91
}


table(as.numeric(data$isco08)[data$isco08=="Agricultural, forestry and fishery labourers"])
table(as.numeric(data$isco08)[data$isco08=="Fishery and aquaculture labourers"])

table(data$isco08)[553:559]

for(i in c(553:559)){
  data$isco2d[data$isco08_num==i]<-92
}


table(as.numeric(data$isco08)[data$isco08=="Labourers in mining, construction, manufacturing and transpo"])
table(as.numeric(data$isco08)[data$isco08=="Shelf fillers"])

table(data$isco08)[560:572]

for(i in c(560:572)){
  data$isco2d[data$isco08_num==i]<-93
}


table(as.numeric(data$isco08)[data$isco08=="Food preparation assistants"])
table(as.numeric(data$isco08)[data$isco08=="Kitchen helpers"])

table(data$isco08)[573:576]

for(i in c(573:576)){
  data$isco2d[data$isco08_num==i]<-94
}


table(as.numeric(data$isco08)[data$isco08=="Street and related sales and service workers"])
table(as.numeric(data$isco08)[data$isco08=="Street vendors (excluding food)"])

table(data$isco08)[577:579]

for(i in c(577:579)){
  data$isco2d[data$isco08_num==i]<-95
}


table(as.numeric(data$isco08)[data$isco08=="Refuse workers and other elementary workers"])
table(as.numeric(data$isco08)[data$isco08=="Elementary workers not elsewhere classified"])

table(data$isco08)[580:590]

for(i in c(580:590)){
  data$isco2d[data$isco08_num==i]<-96
}

table(data$isco2d)


##########################
#Data file for analysis
##########################


table(our$cntry)
table(data$cntry)

our$cntry[our$cntry=="UK"] <- "GB"


data_sub <- merge(data, our, by=c("isco2d","cntry"), all=TRUE)



data_sub %>%
  dplyr::group_by(cntry,essround,isco2d) %>%
  dplyr::summarise(our=mean(our,na.rm=T)) -> df


##########################
#Save data
##########################

#Combine ESS with benefit concentration indicator
save(data_sub,file="DataFile_08_our.Rda")

##########################